//
//  HfAccountViewController.m
//  ChengHuiTong
//
//  Created by ypwl on 15/12/4.
//  Copyright © 2015年 张文权. All rights reserved.
//

#import "HfAccountViewController.h"
#import "YPAccountTableViewCell.h"

@interface HfAccountViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *hfAccountDetailsTableView;
@property (nonatomic,strong)UIControl *filterView;

@end

@implementation HfAccountViewController{
    
    //数据源数组
    NSMutableArray*_dataArr;
    
    NSInteger _page;
    
    NSString *_option; //筛选条件
    
    UIButton *_selectBtn;
    
    UIButton *_filterBtn;
    
}

- (UIControl *)filterView{
    
    if (!_filterView) {
        _filterView = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
        _filterView.backgroundColor = [UIColor colorWithWhite:0. alpha:0.0];
        [_filterView addTarget:self action:@selector(showHiddenFilter) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *contentView  = [[UIView alloc]initWithFrame:CGRectMake(0, -autoScaleH(123), SCREEN_WIDTH, autoScaleH(123))];
        contentView.tag = 999;
        contentView.backgroundColor = [UIColor whiteColor];
        [_filterView addSubview:contentView];
        
        NSArray *titles = @[@"全部",@"充值",@"提现",@"投资",@"收益"];
        CGFloat btnW = (_filterView.width - leftMargin*2 - autoScaleW(10)*2)/3.f;
        CGFloat btnH = autoScaleH(30);
        CGFloat topBottomSpace = autoScaleH(23);
        CGFloat middleSpace = autoScaleH(17);
        [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            UIButton *btn = [UIButton creatWithFrame:CGRectMake(leftMargin+(btnW+autoScaleW(10))*(idx%3), topBottomSpace+(btnH + middleSpace)*(idx/3), btnW, btnH)
                                                font:[UIFont systemFontOfSize:16]
                                                text:obj
                                              action:@selector(filterClick:)
                                              target:self];
            btn.tag = 1000+idx;
            [btn setTitleColor:kColor_greyAshTitleColor forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            btn.layer.cornerRadius = 3.f;
            [btn setBorderWithColor:[UIColor lightGrayColor] borderWidth:1];
            [contentView addSubview:btn];
            
            if (idx == 0) {
                _selectBtn = btn;
                btn.selected = YES;
                btn.backgroundColor = kColor_AppNavColor;
                btn.layer.borderColor = kColor_AppNavColor.CGColor;
            }
        }];
    }
    
    return _filterView;
}

-(instancetype)initWithInforMation:(NSDictionary*)dic{
    
    self=[super init];
    if (self) {
        
        _inforationDic=dic;
        
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"资金明细";
    
    _page=1;
    
    _option = @"0";
    
    _dataArr=[[NSMutableArray alloc] init];
    
    [self creatTableView];
    
    _filterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_filterBtn addTarget:self action:@selector(showHiddenFilter) forControlEvents:UIControlEventTouchUpInside];
    [_filterBtn setBackgroundImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateNormal];
    [_filterBtn setBackgroundImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateHighlighted];
    _filterBtn.size = _filterBtn.currentBackgroundImage.size;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_filterBtn];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    [MobClick beginLogPageView:@"资金明细"];
    
}

- (void) viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    [MobClick endLogPageView:@"资金明细"];
}

#pragma mark 网路请求

- (void)starRequest{
    
    NSLog(@"page:%ld -----  option:%@",(long)_page,_option);
    [YPHttpRequestMethod sendAsyRequestURL:CMD_FFACCOUNTDETAIL withData:@{@"token":TOKEN,@"page":@(_page),@"option":_option} withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        [_hfAccountDetailsTableView.header endRefreshing];
        [_hfAccountDetailsTableView.footer endRefreshing];
        if ([responseDictionary[@"status"] integerValue] == 0) {
            
            if (_page == 1) [_dataArr removeAllObjects];
            
            for (NSDictionary *dic in responseDictionary[@"list"]) {
                
                [_dataArr addObject:[[AccountDetails alloc]initWithDic:dic]];
            }
            
            if ([responseDictionary[@"list"] count] < 5) {
                [_hfAccountDetailsTableView.footer noticeNoMoreData];
            }
            
            self.hfAccountDetailsTableView.footer.hidden = _dataArr.count==0?YES:NO;
            if (self.hfAccountDetailsTableView.footer.hidden) {
                _hfAccountDetailsTableView.tableFooterView = [SelectView footViewWithheight:_hfAccountDetailsTableView.height];
            }else{
                _hfAccountDetailsTableView.tableFooterView = [UIView new];
            }
            
            [_hfAccountDetailsTableView reloadData];
        }
        
    } withFailedBlock:^(NSError *error) {
        
        [_hfAccountDetailsTableView.header endRefreshing];
        [_hfAccountDetailsTableView.footer endRefreshing];
        
    }withIndicator:NO  atView:nil];
    
}

#pragma mark UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArr.count;
    
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YPAccountTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell=[[YPAccountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        cell.selectionStyle=0;
    }
    AccountDetails*detailModel=_dataArr[indexPath.row];
    cell.accountModel=detailModel;
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 65;
    
}

#pragma mark - 私有方法

- (void)creatTableView{
    
    _hfAccountDetailsTableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)];
    headView.backgroundColor =  UIColorFromRGB(0xf5f5f5);
    _hfAccountDetailsTableView.tableHeaderView = headView;
    _hfAccountDetailsTableView.delegate=self;
    _hfAccountDetailsTableView.dataSource=self;
    _hfAccountDetailsTableView.tableFooterView = [UIView new];
    [self.view addSubview:_hfAccountDetailsTableView];
    
    WeakSelf
    _hfAccountDetailsTableView.footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        StrongSelf
        strongSelf->_page ++;
        [strongSelf starRequest];
    }];
    
    [_hfAccountDetailsTableView addGifHeaderWithRefreshingBlock:^{
        StrongSelf
        strongSelf->_page = 1;
        [strongSelf starRequest];
    }];
    
    [_hfAccountDetailsTableView.header beginRefreshing];
    
}

#pragma mark - 事件响应

- (void)showHiddenFilter{
    _filterBtn.enabled = NO;
    _filterBtn.selected = !_filterBtn.selected;
    UIView *contentView = [self.filterView viewWithTag:999];
    if (_filterBtn.selected) {
        
        if (![self.view.subviews containsObject:self.filterView]) [self.view addSubview:self.filterView];
        
        self.filterView.hidden = NO;
        
        [UIView animateWithDuration:0.3 animations:^{
            contentView.top = 0;
            _filterView.backgroundColor = [UIColor colorWithWhite:0. alpha:0.6];
        }completion:^(BOOL finished) {
            _filterBtn.enabled = YES;
        }];
        
    }else{
        
        [UIView animateWithDuration:0.3 animations:^{
            contentView.bottom = 0;
            _filterView.backgroundColor = [UIColor colorWithWhite:0. alpha:0.0];
        }completion:^(BOOL finished) {
            self.filterView.hidden = YES;
            _filterBtn.enabled = YES;
        }];
    }
}

- (void)filterClick:(UIButton *)btn{
    
    if (btn == _selectBtn) {
        return;
    }
    btn.selected = YES;
    btn.backgroundColor = kColor_AppNavColor;
    btn.layer.borderColor = kColor_AppNavColor.CGColor;
    
    _selectBtn.selected = NO;
    _selectBtn.backgroundColor = [UIColor whiteColor];
    _selectBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _selectBtn = btn;
    
    _option = [NSString stringWithFormat:@"%ld",btn.tag - 1000];
    [self.hfAccountDetailsTableView.header beginRefreshing];
    
    [self showHiddenFilter];
}

@end
